import 'package:flutter_app/common/dto/dto.dart';
import 'package:flutter_app/common/utils/utils.dart';

/// 新闻
class NewsAPI {
  /// 翻页
  /// refresh 是否刷新
  static Future<NewsPageListResponse> newsPageList({
    NewsPageListRequest? params,
    bool refresh = false,
    bool cacheDisk = false,
  }) async {
    var response = await HttpUtil().get(
      '/news',
      params: params?.toJson(),
      refresh: refresh,
      cacheDisk: cacheDisk,
    );
    return NewsPageListResponse.fromJson(response);
  }

  /// 推荐
  static Future<NewsRecommendResponse> newsRecommend({
    NewsRecommendRequest? params,
    bool refresh = false,
    bool cacheDisk = false,
  }) async {
    var response = await HttpUtil().get(
      '/news/recommend',
      params: params?.toJson(),
      refresh: refresh,
      cacheDisk: cacheDisk,
    );
    return NewsRecommendResponse.fromJson(response);
  }

  /// 分类
  static Future<List<CategoryResponse>> categories({
    bool cacheDisk = false,
  }) async {
    var response = await HttpUtil().get(
      '/categories',
      cacheDisk: cacheDisk,
    );
    return response.map<CategoryResponse>((item) => CategoryResponse.fromJson(item)).toList();
  }

  /// 频道
  static Future<List<ChannelResponse>> channels({
    bool cacheDisk = false,
  }) async {
    var response = await HttpUtil().get(
      '/channels',
      cacheDisk: cacheDisk,
    );
    return response
        .map<ChannelResponse>(
            (item) => ChannelResponse.fromJson(item))
        .toList();
  }

  /// 标签列表
  static Future<List<TagResponse>> tags({
    TagRequest? params,
    bool cacheDisk = false,
  }) async {
    var response = await HttpUtil().get(
      '/tags',
      params: params?.toJson(),
      cacheDisk: cacheDisk,
    );
    return response
        .map<TagResponse>((item) => TagResponse.fromJson(item))
        .toList();
  }
}
